﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FDStudio.Core.Entities.Enums;
using FDStudio.Core.Runtime;

namespace FDStudio.Core.Caches
{
    /*
     CreatedOn: 2018-02-08
     CreatedBy: Jackie Lee(天宇遊龍)
     */
    /// <summary>
    /// 默认缓存
    /// </summary>
    public class DefaultCache : ICache
    {
        private ICacheContext _context;

        public DefaultCache()
        {
            _context = RuntimeContext.Current.Container.Resove<ICacheContext>();
        }

        private CacheType cacheType;

        /// <summary>
        /// 缓存类型
        /// </summary>
        public CacheType CacheType
        {
            get
            {
                return cacheType;
            }
            set
            {
                cacheType = value;
                _context = RuntimeContext.Current.Container.ResoveAll<ICacheContext>()
                    .Where(c => c.CacheType == cacheType).FirstOrDefault();
                if (_context == null)
                    throw new NotSupportedException($"未找到{cacheType}类型的缓存上下文");
            }
        }

        public string Get(string key)
        {
            return _context.Get(key);
        }

        public T Get<T>(string key, string field)
        {
            return _context.Get<T>(key, field);
        }

        public T Get<T>(string key)
        {
            return _context.Get<T>(key);
        }

        //public IList<T> Get<T>(string key)
        //{
        //    return _context.Get<T>(key);
        //}

        public void Remove(string key)
        {
            _context.Remove(key);
        }

        public void Remove(string key, string field)
        {
            _context.Remove(key, field);
        }

        public void Remove<T>(string key, T model)
        {
            _context.Remove(key, model);
        }

        public void Set(string key, string value, int expires = 0)
        {
            _context.Set(key, value, expires);
        }

        public void Set<T>(string key, string field, T model, int expires = 0)
        {
            _context.Set<T>(key, field, model, expires);
        }

        public void Set<T>(string key, T model, int expires = 0)
        {
            _context.Set(key, model, expires);
        }

        //public void Set<T>(string key, IList<T> values, int expires = 0)
        //{
        //    _context.Set(key, values);
        //}

        public void Switch(string name)
        {
            _context.Switch(name);
        }

        public void SwitchDatabase(int databaseIndex)
        {
            _context.SwitchDatabase(databaseIndex);
        }
    }
}
